<template>
  <el-dialog v-loading="loading" :show-close="false" width="800px" title="选择消息模板" :visible.sync="visible" @cancel="close">
    <el-table :data="list" border>
      <el-table-column prop="title" align="center" label="标题" />
      <el-table-column prop="templateIdShort" align="center" label="模板ID" />
      <el-table-column prop="templateType" align="center" label="模板类型">
        <template slot-scope="scope">
          <span v-if="scope.row.templateType === 0">公众号</span>
          <span v-if="scope.row.templateType === 1">小程序</span>
        </template>
      </el-table-column>
      <el-table-column prop="description" align="center" label="说明" />
      <el-table-column align="center" label="操作" width="150px">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="handleSelect(scope.row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 10px "
      background
      :current-page.sync="pageNum"
      :page-size.sync="pageSize"
      layout="total, prev, pager, next"
      :total="total"
      @current-change="handleCurrentChange"
    />
  </el-dialog>
</template>

<script>
import { listTemplate } from '@/api/wxTemplateMsg'
export default {
  name: 'TemplateMsgDialog',
  data() {
    return {
      loading: false,
      visible: false,
      reject: null,
      resolve: null,
      pageNum: 0,
      pageSize: 10,
      list: [],
      total: 0
    }
  },
  mounted() {
    this.getList()
  },

  methods: {
    getList() {
      listTemplate({ pageNum: this.pageNum, pageSize: this.pageSize }).then((res) => {
        this.list = res.data.list
        this.total = res.data.total
        return res
      })
    },

    handleCurrentChange() {
      this.getList()
    },

    handleSelect(template) {
      const resolve = this.resolve
      this.resolve = null
      this.reject = null
      resolve && resolve(template)
      this.visible = false
    },

    show() {
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
        this.visible = true
        this.getList()
      })
    },
    close() {
      this.resolve = null
      this.reject = null
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .search-container {
    margin-top: 20px;
    margin-bottom: 20px
  }

  .search-wrap {
    width: 500px;
    margin-right: 30px
  }
</style>
